<template>
    <el-dialog
        :title="'pc sdk热更新配置'"
        :visible="visible"
        width="550px"
        :destroy-on-close="true"
        :lock-scroll="false"
        @update:visible="updateVisible"
        :close-on-click-modal="false"
    >
        <el-form
            ref="form"
            :model="form"
            :rules="rules"
            label-width="100px"
            >
            <el-form-item label="游戏包名称:" prop="package_name">
                <el-input
                    :disabled="true"
                    v-model="form.package_name"
                    :placeholder="'请输入游戏包名称'"
                    clearable
                />
            </el-form-item>
            <el-form-item label="最新版本:" prop="new_version">
                <el-input
                    v-model="form.new_version"
                    :placeholder="'请输入最新版本'"
                    clearable
                />
            </el-form-item>
            <el-form-item label="更新地址:" prop="dow_url">
                <el-input
                    v-model="form.dow_url"
                    :placeholder="'请输入更新地址'"
                    clearable
                />
            </el-form-item>
            <el-form-item label="是否开启:" prop="status">
                <el-radio-group v-model="form.status">
                    <el-radio :label="0">关闭</el-radio>
                    <el-radio :label="1">开启</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="更新内容:">
                <el-input
                    v-model="form.content"
                    :placeholder="'请输入更新内容'"
                    clearable
                    type="textarea"
                    :row="5"
                />
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="updateVisible(false)">取消</el-button>
            <el-button type="primary" @click="save" :loading="loading"
                >保存
            </el-button>
        </div>     
    </el-dialog>
</template>
<script>
export default {
    props: {
        // 弹窗是否打开
        visible: Boolean,
        // 修改回显的数据
        data: Object
    },
    data(){
        return{
            // 表单数据
            form: Object.assign({}),
            // 表单验证规则
            rules: {
                package_name: [
                { required: true, message: "请输入游戏包名称", trigger: "blur" }
                ],
                new_version: [
                { required: true, message: "请输入最新版本", trigger: "blur" }
                ],
                dow_url: [
                { required: true, message: "请输入更新地址", trigger: "blur" }
                ],
                status: [
                { required: true, message: "请选择是否开启", trigger: "blur" }
                ],
            },
            loading:false,
            packageId:"",
        }
    },
    watch:{
        visible(){
            if (this.data && this.visible){
                this.form=Object.assign({
                    package_name:this.data.package_name,
                    new_version:"",
                    dow_url:"",
                    status:0,
                    content:"",
                })
                this.packageId=this.data.id
                this.$http.get('/package/getPackageRenewConfig?package_id='+this.packageId).then(res=>{
                    if(res.data.code === 0){
                        if(res.data.data){
                            this.form.new_version=res.data.data.new_version;
                            this.form.dow_url=res.data.data.dow_url;
                            this.form.status=res.data.data.status;
                            this.form.content=res.data.data.content;
                        }
                    }else{
                        this.$message.error(res.data.msg);
                    }
                })
            }else{
                this.form = {};
                this.save_form = {};
                this.isUpdate = false;
            }
        }
    },
    created(){
        
    },
    mounted(){

    },
    methods:{
        /* 更新visible */
        updateVisible(value) {
            this.$emit("update:visible", value);
        },
        //保存
        save(){
            this.$refs["form"].validate(valid => {
                if(valid){
                    this.loading = true;
                    let params={
                        package_id:this.packageId,
                        new_version:this.form.new_version,
                        dow_url:this.form.dow_url,
                        status:this.form.status,
                        content:this.form.content
                    }
                    this.$http.post('/package/setPackageRenewConfig',params).then(res=>{
                        if(res.data.code=== 0){
                            this.$message.success(res.data.msg);
                            this.form = {};
                            this.updateVisible(false);
                            this.$emit("done");
                            this.loading=false;
                        }else{
                            this.$message.error(res.data.msg);
                        }
                    }).catch(e => {
                        this.loading = false;
                        this.$message.error(e.message);
                    });
                }
            })
        },
    },
}
</script>
<style scoped>

</style>